import axios from "axios"
import * as types from "../actionTypes"
// 创建一个action creator
const setChannel = (payload) => ({ type: types.GETCHANNEL, payload })
export const toggleChannel = (payload) => ({
  type: types.TOGGLEACTIVE,
  payload,
})

export const getChannel = () => {
  return async (dispatch, getState) => {
    // 做异步请求
    const {
      data: {
        data: { channels },
      },
    } = await axios({
      url: "http://toutiao.itheima.net/v1_0/channels",
    })
    // 拿到channels
    dispatch(setChannel(channels))
    // 拿到了所有的数据 - 将第一个的id激活
    dispatch(toggleChannel(channels[0].id))
  }
}
